﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace NDepot.Models.UI
{
    public class CartInput
    {
        public IList<ProductInput> Products { get; set; }
        public Cart Cart { get; set; }
    }

    public class Cart
    {
        public IDictionary<string, CartItem> Items { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C}")]
        public decimal TotalPrice
        {
            get
            {
                decimal totalPrice = 0;
                foreach (CartItem item in Items.Values)
                    totalPrice += item.Price;

                return totalPrice;
            }
        }

        public Cart()
        {
            Items = new Dictionary<string, CartItem>();
        }

        public void AddProduct(ProductInput product)
        {
            if (Items.Keys.Contains(product.Title))
                Items[product.Title].Quantity += 1;
            else
                Items.Add(product.Title, new CartItem(product));

            CurrentItem = Items[product.Title];
        }

        public CartItem CurrentItem { get; set; }
    }

    public class CartItem
    {
        private ProductInput _Product { get; set; }

        public decimal Price
        {
            get
            {
                return _Product.Price * Quantity;
            }
        }

        public string ProductId { get { return _Product.Id; } }
        public int Quantity { get; set; }
        public string Title { get { return _Product.Title; } }

        public CartItem(ProductInput product)
        {
            _Product = product;
            Quantity = 1;
        }
    }
}